<!--
 Copyright 2025 Google LLC

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->

<div class="chat-container">
  <div class="chat-body">

    <!-- Suggestion Card (Global or per message) -->
    <div class="bot-message" *ngIf="showSuggestion && suggestedQuestionMessage?.suggestedQuestion?.length" class="bot-suggestion-container">
      <div class="bot-suggestion-container-child">
        <app-suggestion-card [data]="getSuggestionData(this.suggestedQuestionMessage)"
                             (raiseQuery)="getResponseforSuggestionQuery($event)"
                             (dismissSuggestionCard)="removeSuggestionElement()"></app-suggestion-card>
      </div>
    </div>

    <!-- Loader -->
    <div *ngIf="showLoader">
      <div class="loader">
        <div class="{{leftContainerClass}}"></div>
        <div class="loader-child-container {{rightContainerClass}}">
          <div [ngClass]="showLoaderLikeDislikeButtons? 'loader-text-container':''">
            <markdown class="variable-binding" [data]="loaderText"></markdown>
          </div>
          <div *ngIf="showLoaderLikeDislikeButtons" class="like-dislike-button">
          </div>
        </div>
      </div>
    </div>

    <!-- Chat section for the messages -->
    <ng-container *ngFor="let message of conversation;index as i">

      <div class="user-message" *ngIf="message.type == 'user'">
        <div class="user-message-wrapping-container">
          <div class="messageContent">
            <!-- User messages will have one contentPart of type text -->
            <ng-container *ngIf="message.contentParts && message.contentParts.length > 0 && message.contentParts[0].type === 'text'">
              {{message.contentParts[0].text}}
            </ng-container>
          </div>
          <div class="user-circle" *ngIf="requiredLogin === 'True' && userService.getUserDetails().photoURL">
            <img [src]="userService.getUserDetails().photoURL" class="logo-icon" />
          </div>
          <div *ngIf="requiredLogin === 'False'" class="user-icon">
            <mat-icon class="bypassMenuLogo">person</mat-icon>
          </div>
        </div>
      </div>

      <div class="bot-message"
           *ngIf="message.type == 'bot'"
           [attr.id]="message.botStartTime">
        <div class="bot-message-wrapping-container">
          <div class="bot-message-action">
            <div></div>
          </div>

          <div class="bot-message-content">
            <!-- Iterate over content parts for bot messages -->
            <ng-container *ngFor="let part of message.contentParts">
              <!-- Thinking Block -->
              <details *ngIf="part.type === 'thinking' && part.thinkingSteps && part.thinkingSteps.length > 0" class="thinking-dropdown">
                <summary *ngIf="!['Memorize', 'Transfer To Agent'].includes((part.thinkingSteps?.[0]?.name | formatAgentName))" class="agent-chip-container" style="margin-bottom: 0.5em;">
                  <mat-chip color="primary" selected>
                    {{ (part.thinkingSteps?.[0]?.name | formatAgentName) }}
                  </mat-chip>
                </summary>
                <summary *ngIf="(part.thinkingSteps?.[0]?.name | formatAgentName) === 'Transfer To Agent'">
                  <mat-chip color="primary" selected>
                    Travel Concierge
                  </mat-chip>
                   will transfer you to <b>{{ part.thinkingSteps?.[0]?.data?.agent_name | formatAgentName }}...</b>
                </summary>
                <summary *ngIf="(part.thinkingSteps?.[0]?.name | formatAgentName) === 'Memorize'">
                  <mat-chip color="primary" selected>
                    Travel Concierge
                  </mat-chip> 
                  is memorizing the input...
                </summary>
                <div class="function-details-container">
                  <ng-container *ngFor="let step of part.thinkingSteps">
                    <div *ngIf="step.type === 'functionCall'" class="function-step">
                      <strong>Function Call:</strong> {{ step.name }} <br>
                      <strong>Arguments:</strong> <pre>{{ step.data | json }}</pre>
                    </div>
                    <div *ngIf="step.type === 'functionResponse'" class="function-step">
                      <strong>Function Response:</strong> {{ step.name }} <br>
                      <strong>Response:</strong> <pre>{{ step.data | json }}</pre>
                    </div>
                  </ng-container>
                </div>
              </details>

              <ng-container *ngIf="part.type === 'thinking' && part.thinkingSteps?.[1]?.type === 'functionResponse' && 
                ['Map Tool', 'Poi Agent'].includes((part.thinkingSteps?.[1]?.name  | formatAgentName))">
                <!-- Custom Display for Map Tool Response -->
                <h6 style="margin-top: 0.5em; margin-bottom: 0.5em;">Places Found:</h6>
                <div *ngIf="part.thinkingSteps?.[1]?.data?.places?.length > 0" class="map-tool-response">
                  <div *ngFor="let place of part.thinkingSteps?.[1]?.data.places" class="place-card" style="margin-bottom: 2rem;">
                    <!-- TODO: Once the place.image_url returns valid images, change back to use this prop -->
                    <!-- <img *ngIf="place.image_url" [src]="place.image_url" [alt]="place.place_name" class="place-image"> -->
                    <img *ngIf="place.place_name || place.image_url"
                        [src]="getPlaceDisplayUrl(place)"
                        [alt]="place.place_name || 'Location image'"
                        class="place-image">
                    <div class="place-details">
                      <h5>{{ place.place_name }}</h5>
                      <p *ngIf="place.address"><strong>Address:</strong> {{ place.address }}</p>
                      <p *ngIf="place.highlights"><strong>Highlights:</strong> {{ place.highlights }}</p>
                      <p *ngIf="place.review_ratings"><strong>Rating:</strong> {{ place.review_ratings }} / 5</p>
                      <a mat-stroked-button color="primary"
                        [href]="'https://www.google.com/maps?q=' + place.lat + ',' + place.long"
                        target="_blank"
                        rel="noopener noreferrer"
                        class="map-button">
                        <mat-icon>map</mat-icon> View on Maps
                      </a>
                    </div>
                  </div>
                </div>
              </ng-container>

              <!-- Text Block -->
              <markdown *ngIf="part.type === 'text' && part.text" clipboard class="variable-binding" [data]="part.text"></markdown>
            </ng-container>

            <div class="action-content" *ngIf="message.shareable">
              <div class="action-message"></div>
              <div class="action-buttons">
              </div>
            </div>
          </div>

        </div>
        <span class="generating-text" *ngIf="message.responseTime">Generated in {{message.responseTime}}s</span>
        <div *ngIf="message.suggestedQuestion && message.suggestedQuestion.length > 0" class="bot-suggestion-chips">
          <mat-chip-listbox aria-label="Suggested questions">
            <mat-chip-option *ngFor="let suggestion of message.suggestedQuestion" (click)="getResponseforSuggestionQuery(suggestion)">
              {{suggestion}}
            </mat-chip-option>
          </mat-chip-listbox>
        </div>
      </div>
    </ng-container>

  </div>
  <div class="chat-footer-outer">
    <div [ngStyle]="{'opacity':this.isChatDisabled ? '30%' : '100%' }" class="chat-footer">
      <div class="chat-textarea">
        <textarea [disabled]="this.isChatDisabled"
                  [placeholder]="!this.isChatDisabled ?  'Type your question here, make sure to add context' : ''"
                  [(ngModel)]="chatQuery" (keydown.enter)="submitMessage($event)"
                  (paste)="removeSuggestionElement();"
                  (keypress)="removeSuggestionElement();">
        </textarea>
      </div>
      <div class="micIcon">
        <button mat-icon-button (click)="startRecording()" matTooltip="Record"
                *ngIf="!isRecording"><mat-icon>mic_none</mat-icon>
        </button>
        <span (click)="stopRecording()" class="recording-container" *ngIf="isRecording">
          <span class="blue-dot">&nbsp;</span>
          <span class="green-dot">&nbsp;</span>
          <span class="yellow-dot">&nbsp;</span>
          <button (click)="stopRecording()" style="color: red;" class="reset-button initial" mat-icon-button
                  matTooltip="Stop Recording" aria-label="record button">
            <mat-icon class="material-icons-outlined">stop_circle</mat-icon> </button>
        </span>
        <button (click)="resetBrowser()" class="reset-button initial" mat-icon-button aria-label="Share icon button">
          <span class="material-symbols-rounded send-icon">refresh</span>
        </button>

        <button mat-icon-button (click)="submitMessage($event)">
          <mat-icon class="send-icon">send</mat-icon>
        </button>
      </div>
    </div>
  </div>
</div>
